#!/usr/bin/env node
const { writeFile, readFile, copyFile } = require('fs')
const { promisify } = require('util')

/* 修复react-native-linear-gradient */
require('../node_modules/react-native-text-gradient/patch-rn.js')

/* iOS input 中文输入 */
copyFile('patch/RCTBaseTextInputView.m', 'node_modules/react-native/Libraries/Text/TextInput/RCTBaseTextInputView.m', (err) => {
  if (err) throw err
  console.log('iOS input 中文输入 修复完成 ----->   RCTBaseTextInputView.m ')
})

/* iOS13 状态栏 */
copyFile('patch/RCTStatusBarManager.m', 'node_modules/react-native/React/Modules/RCTStatusBarManager.m', (err) => {
  if (err) throw err
  console.log('iOS13 状态栏  修复完成 ----->   RCTStatusBarManager.m ')
})
/* Android 微信 */
const needFixFiles = [
  /* Android 微信 */
  {
    file: 'node_modules/react-native-wechat-with-miniprogram-share/android/src/main/java/com/theweflex/react/WeChatModule.java',
    pattern: /import androidx.annotation.Nullable;/,
    result: 'import android.support.annotation.Nullable;'
  },
  {
    file: 'node_modules/react-native-wechat-with-miniprogram-share/android/build.gradle',
    pattern: /implementation/,
    result: 'compile'
  },
  {
    file: 'node_modules/react-native-image-crop-picker/ios/src/ImageCropPicker.m',
    pattern: /"Cancel"/g,
    result: '"取消"'
  },
  {
    file: 'node_modules/react-native-image-crop-picker/ios/src/ImageCropPicker.m',
    pattern: /"Choose"/g,
    result: '"选择"'
  },
  {
    file: 'node_modules/react-native-image-crop-picker/ios/src/ImageCropPicker.m',
    pattern: /"Processing assets..."/g,
    result: '""'
  },
  /* ios react-native-orientation-locker  UIInterfaceOrientationMaskAll */
  // {
  //   file: 'node_modules/react-native-orientation-locker/iOS/RCTOrientation/Orientation.m',
  //   pattern: /UIInterfaceOrientationMaskAll/,
  //   result: 'UIInterfaceOrientationMaskAllButUpsideDown'
  // },
  /* ios PolyvVodSDK  sqlite3CodecAttach */
  {
    file: 'ios/Pods/WCDBOptimizedSQLCipher/src/attach.c',
    pattern: /sqlite3CodecAttach/g,
    result: 'plv_qlite3CodecAttach'
  },
  {
    file: 'ios/Pods/WCDBOptimizedSQLCipher/src/crypto.c',
    pattern: /sqlite3CodecAttach/g,
    result: 'plv_qlite3CodecAttach'
  },
  {
    file: 'ios/Pods/WCDBOptimizedSQLCipher/src/crypto_impl.c',
    pattern: /sqlite3CodecAttach/g,
    result: 'plv_qlite3CodecAttach'
  },
  /* ios PolyvVodSDK  sqlite3CodecGetKey */
  {
    file: 'ios/Pods/WCDBOptimizedSQLCipher/src/attach.c',
    pattern: /sqlite3CodecGetKey/g,
    result: 'plv_qlite3CodecGetKey'
  },
  {
    file: 'ios/Pods/WCDBOptimizedSQLCipher/src/crypto.c',
    pattern: /sqlite3CodecGetKey/g,
    result: 'plv_qlite3CodecGetKey'
  },
  {
    file: 'ios/Pods/WCDBOptimizedSQLCipher/src/crypto_impl.c',
    pattern: /sqlite3CodecGetKey/g,
    result: 'plv_qlite3CodecGetKey'
  },
  {
    file: 'ios/Pods/WCDBOptimizedSQLCipher/src/vacuum.c',
    pattern: /sqlite3CodecGetKey/g,
    result: 'plv_qlite3CodecGetKey'
  },
  /* ios PolyvVodSDK  aes_encrypt_key128 */
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aes.h',
    pattern: /aes_encrypt_key128/g,
    result: 'plv_es_encrypt_key128'
  },
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aeskey.c',
    pattern: /aes_encrypt_key128/g,
    result: 'plv_es_encrypt_key128'
  },
  /* ios PolyvVodSDK  aes_decrypt_key128 */
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aes.h',
    pattern: /aes_decrypt_key128/g,
    result: 'plv_es_decrypt_key128'
  },
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aeskey.c',
    pattern: /aes_decrypt_key128/g,
    result: 'plv_es_decrypt_key128'
  },
  /* ios PolyvVodSDK  aes_encrypt_key192 */
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aes.h',
    pattern: /aes_encrypt_key192/g,
    result: 'plv_es_encrypt_key192'
  },
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aeskey.c',
    pattern: /aes_encrypt_key192/g,
    result: 'plv_es_encrypt_key192'
  },
  /* ios PolyvVodSDK  aes_decrypt_key192 */
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aes.h',
    pattern: /aes_decrypt_key192/g,
    result: 'plv_es_decrypt_key192'
  },
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aeskey.c',
    pattern: /aes_decrypt_key192/g,
    result: 'plv_es_decrypt_key192'
  },
  /* ios PolyvVodSDK  aes_encrypt_key256 */
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aes.h',
    pattern: /aes_encrypt_key256/g,
    result: 'plv_es_encrypt_key256'
  },
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aeskey.c',
    pattern: /aes_encrypt_key256/g,
    result: 'plv_es_encrypt_key256'
  },
  /* ios PolyvVodSDK  aes_decrypt_key256 */
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aes.h',
    pattern: /aes_decrypt_key256/g,
    result: 'plv_es_decrypt_key256'
  },
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aeskey.c',
    pattern: /aes_decrypt_key256/g,
    result: 'plv_es_decrypt_key256'
  },
  /* ios PolyvVodSDK  define aes_xi\(x\) aes_ */
  {
    file: 'ios/Pods/SSZipArchive/SSZipArchive/minizip/aes/aeskey.c',
    pattern: /define aes_xi\(x\) aes_/g,
    result: 'define aes_xi(x) plv_es_'
  }
]
const patchFile = async ({ file, pattern, result }) => {
  console.log('begin')
  const content = (await promisify(readFile)(file)).toString()
  const patched = content.replace(pattern, result)
  await promisify(writeFile)(file, patched)
  console.log('end')
}
const patchAll = async () => {
  // await Promise.all(needFixFiles.map(patchFile))
  for (const [idx, item] of needFixFiles.entries()) {
    await patchFile(item)
    console.log(idx, 'done')
  }
  return ' all done'
}
patchAll().then((data) => {
  console.info(data)
})
